//
//  HTTPRequestTool.m
//  TestByFF
//
//  Created by hanxifeng on 16/7/13.
//  Copyright © 2016年 haixu. All rights reserved.
//  afnetworking二次封装

#import "HTTPRequestTool.h"
#import "AFNetworking.h"

@implementation HTTPRequestTool

/**GET请求*/
+(void)GET:(NSString *)urlStr params:(NSDictionary *)paramsDic success:(successBlock)success failure:(failureBlock)failure{
    AFHTTPSessionManager *manager = [self initHTTPSessionManage];
    [manager GET:urlStr parameters:paramsDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //返回的数据JSON格式
        id JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //返回的数据String格式
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        success(JSON,responseStr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

/**POST请求*/
+(void)POST:(NSString *)urlStr params:(NSDictionary *)paramsDic success:(successBlock)success failure:(failureBlock)failure{
    AFHTTPSessionManager *manager = [self initHTTPSessionManage];
    [manager POST:urlStr parameters:paramsDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //返回的数据JSON格式
        id JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //返回的数据String格式
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        success(JSON,responseStr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

/**上传图片  POST请求*/
+(void)UploadPhoto:(NSString *)urlStr params:(NSDictionary *)paramsDic files:(NSDictionary *)filesDic progress:(progressBlock)progress success:(successBlock)success failure:(failureBlock)failure{
    AFHTTPSessionManager *manager = [self initHTTPSessionManage];
    [manager POST:urlStr parameters:paramsDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSArray *fileKeys = [filesDic allKeys];
        for (int i=0; i<fileKeys.count; i++) {
            //文件名
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg",[formatter stringFromDate:[NSDate date]]];
            //文件key
            NSString *fileKey = fileKeys[i];
            //文件data
            NSData *fileData = [filesDic objectForKey:fileKey];
            //拼接上传的文件
            [formData appendPartWithFileData:fileData name:fileKey fileName:fileName mimeType:@"image/png"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //当前上传大小 占 总大小的比
        float progressFloat = (float)uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
        progress(uploadProgress,progressFloat);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //返回的数据JSON格式
        id JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //返回的数据String格式
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        success(JSON,responseStr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+(void)UploadFile:(NSString *)urlStr params:(NSDictionary *)paramsDic files:(NSArray *)filesArray progress:(progressBlock)progress success:(successBlock)success failure:(failureBlock)failure{
    AFHTTPSessionManager *manager = [self initHTTPSessionManage];
    [manager POST:urlStr parameters:paramsDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        for (int i=0; i<filesArray.count; i++) {
            //文件名
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg",[formatter stringFromDate:[NSDate date]]];
            //文件key
            NSString *fileKey = [filesArray[i] objectForKey:@"fileKey"];
            //文件data
            NSData *fileData = [filesArray[i] objectForKey:@"fileValue"];
            //文件类型
            NSString *tempContentType = [filesArray[i] objectForKey:@"fileContentType"];
            NSString *fileContentType = tempContentType==nil?@"image/png":tempContentType;
            //拼接上传的文件
            [formData appendPartWithFileData:fileData name:fileKey fileName:fileName mimeType:fileContentType];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //当前上传大小 占 总大小的比
        float progressFloat = (float)uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
        progress(uploadProgress,progressFloat);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //返回的数据JSON格式
        id JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //返回的数据String格式
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        success(JSON,responseStr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+(AFHTTPSessionManager *)initHTTPSessionManage{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10;
    //告诉afn返回数据可以是任意  否则默认是JSON格式 一些非JSON格式的字符串信息就获取不到
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //后台返回的text/html格式
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", @"text/html", nil];
    return manager;
}
@end
